import { Component, OnInit } from '@angular/core';
import {Location} from "@angular/common";
import {HttpService} from '../http.service';
import {Student} from '../student';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  isCollapsed = false;
  Student: Student = {
    stuid : null,
    name: '',
    birthday: '' ,
    class : '',
    major : '' ,
    native_place : '' ,
    sex : '' ,
    time_of_enrollment : '' ,
  };
  stuid = sessionStorage.getItem('user');
  toggleCollapsed(): void {
    this.isCollapsed = !this.isCollapsed;
  }

  constructor(private location: Location,
              private httpService: HttpService,
              private route: Router) { }
  GET() {
    this.httpService.getStudentDetail(this.stuid).subscribe(
      res => this.Student = {
        stuid: res.data.stuid,
        name: res.data.name,
        birthday: res.data.birthday,
        class: res.data.class,
        major: res.data.major,
        native_place: res.data.native_place,
        sex: res.data.sex,
        time_of_enrollment: res.data.time_of_enrollment
      });
  }
  goBack(): void {
    this.location.back();
  }

  ngOnInit(): void {
    this.toggleCollapsed();
    this.toggleCollapsed();
    if (this.stuid) {
      this.GET();
    } else {
      this.route.navigate(['welcome']);
    }
  }

}
